using UnityEngine;
using System;
using System.Collections;

public class BaseAI : MoverBase
{
	public enum CharState{Searching = 1, Chasing = 2, Fighting = 3};
	
	private Vector3 movingDestination;
	private Vector3 movingVelocity;
	
	
	// how close the distance that consider as reached the target
	public float destinationDistance;
	public float movingSpeed;
	public Vector3 movingDirection;
	public GameObject targetCharacter;
	
	public Vector3 tickRotation;
	
	public CharState thisState;
	
	
	// init Destination tolerate, move speed and state
	void Start () {
		customUpdateMoveFunction = updateMove;
		customUpdateAnimationFunction = updateAnimation;
		
		destinationDistance = 1.7f;
		movingSpeed = 13.0f;
		thisState = CharState.Searching;
	}
	
	// change the destination in vector3 that the AI is going to reach
	public void changeDestination(Vector3 des) {
		float dist = Vector3.Distance(transform.position, des);
		if (dist > destinationDistance)
		{
			movingDestination = des;
		//	movingDirection = des - transform.position;
		//	movingVelocity = (movingDirection / dist) * (movingSpeed / 60.0f);
		}
		else
		{
			movingVelocity = new Vector3(0.0f, 0.0f, 0.0f);
		}
	}
	
	// when see an ememy, chase her
	void chaseTarget(MoverBase mb)
	{
		changeDestination(targetCharacter.transform.position);
		moveToDestination(mb);
	}
	
	// do this every frame when searching enemy
	void moveToDestination(MoverBase mb) {
		float dist = Vector3.Distance(transform.position, movingDestination);
		
		CharacterController controller = GetComponent<CharacterController> ();
		
	//	movingDestination = GameObject.Find("CubeTEST").transform.position;
		//print(movingDestination);
		// do not move when too close to the target
		if (dist > destinationDistance)
		{
			movingDirection = (movingDestination - transform.position) / dist;
			movingVelocity = movingDirection;
			//transform.rotation = Quaternion.LookRotation(Vector3.Normalize(movingDirection - transform.position));
			transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.LookRotation(Vector3.Normalize(movingDirection)), Time.time * 0.01f);
			
		//	transform.position += movingDirection;
			if (UtilEx.facing(0.95f, transform.position, movingDestination, transform.rotation, 0)) {
				if (UtilEx.facing(0.0f, transform.position, movingDestination, transform.rotation * Quaternion.Euler(0, 90, 0), 0)) {
					print("QwQ");
				}
			}
		//	Quaternion directionNeeded = Quaternion.LookRotation(movingDestination - transform.position);
		//	print(Quaternion.Angle(transform.rotation, directionNeeded));
		}
		else
		{
			movingVelocity = new Vector3(0.0f, 0.0f, 0.0f);
		}
		
		
		if (controller.isGrounded) {
			
			//reset JumpCount
			mb._jumpCount = 0;
			
			//get Direction from controller (Keyboard)
		//	desiredFacingDirection = new Vector3 (mb.horizontalOffset, 0, mb.verticalOffset);
			
		//	movingVelocity = transform.TransformDirection (desiredFacingDirection);
			
			movingVelocity *= mb.speed;
		}
		
		
		//apply gravity
		movingVelocity.y -= mb.gravity * Time.deltaTime;
		controller.Move (movingVelocity * Time.deltaTime);
	}
	
	void checkFacing(Vector3 target, Quaternion rotation) {
		float facingDifference;
		
		//	Vector3 desiredFacingDirection = new Vector3 (1.0f, 0, 1.0f);
		//	mb._movement = transform.TransformDirection (desiredFacingDirection);
		
	}
	
	protected override bool updateMove (MoverBase mb) {
		switch (thisState) {
		case CharState.Searching: moveToDestination(mb);
			break;
		case CharState.Chasing: chaseTarget(mb);
			break;
		case CharState.Fighting: 
			break;
		}
		return true;
	}
	
	protected override bool updateAnimation() {
		if(state == null)
		{
			state = GetComponent<ActorController>().actorState;
		}
		
		state.animState = movingVelocity.magnitude < walkIdleThreshold ? AnimationState.Idle : AnimationState.Walk;
		return true;
	}
	
	void Update() {
		customUpdateAnimationFunction();
		customUpdateMoveFunction(this);
	}
}